package com.ztgf.user.api.client;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
import com.ztgf.user.models.service.IUserLoginLogService;
import com.ztgf.user.models.bo.UserLoginLogBO;
import com.ztgf.user.models.dto.UserLoginLogDTO;

/**
 * 用户登录日志表控制器
 *
 * @author zhanghualang
 * @Date 2020-05-28 17:44:59
 */

@RestController
@RequestMapping("/api/userLoginLog")
@Slf4j
@Api(value = "UserLoginLogApiController",tags = "用户登录日志表Api接口")
public class UserLoginLogApiController {

    @DubboReference
    private IUserLoginLogService userLoginLogService;

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<UserLoginLogDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        UserLoginLogDTO dto = userLoginLogService.selectById(id);

        return new Result<>(dto);
    }

    @ApiOperation(value = "用户登录日志表列表",notes = "用户登录日志表列表")
    @PostMapping("/queryPageList")
    public Result<Page<UserLoginLogDTO>> queryPageList(UserLoginLogBO bo){
        Wrapper<UserLoginLogDTO> wrapper = new EntityWrapper<>();
        if (bo.getUserName() != null) {
            wrapper.like("user_name",bo.getUserName());
        }

        if (bo.getType() != null) {
            wrapper.eq("type",bo.getType());//登录状态 0登录、1登出
        }

        if (bo.getResultCode() != null) {
            wrapper.eq("result_code",bo.getResultCode());//登录返回值 0成功，其他值失败
        }

        if (bo.getStartDate() != null){
            wrapper.ge("create_time", bo.getStartDate());
        }
        if (bo.getEndDate() != null){
            wrapper.le("create_time", bo.getEndDate());

        }
        Page<UserLoginLogDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = userLoginLogService.selectPage(page, wrapper);

        return new Result<>(page);
    }
}
